package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 获取评论列表
     *
     * @param result
     * @param movementId
     * @return
     */
    @Override
    public PageResult<Comment> getComment(PageResult result, ObjectId movementId) {
        Query query = new Query().addCriteria(Criteria.where("publishId").is(movementId).and("commentType").is(CommentType.COMMENT.getType()));
        long count = mongoTemplate.count(query, Comment.class);
        query.skip((result.getPage() - 1) * result.getPagesize()).limit(Math.toIntExact(result.getPagesize()));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return new PageResult<Comment>(result.getPage(), result.getPagesize(), count, comments);
    }

    /**_
     * 评论动态
     *
     * @return
     */
    @Override
    public void putComment(Comment comments) {
        mongoTemplate.insert(comments, "comment");
        Query query = new Query().addCriteria(Criteria.where("_id").is(comments.getPublishId()));
        Update update = new Update();
        mongoTemplate.updateFirst(query, update.inc("commentCount",1), Movement.class);
    }

    /**
     * 点赞评论
     *
     * @return
     */
    @Override
    public Integer likeComment(ObjectId id) {
        Query query = new Query().addCriteria(Criteria.where("_id").is(id).and("commentType").is(CommentType.COMMENT.getType()));
        Update update = new Update();
        mongoTemplate.updateFirst(query, update.inc("likeCount",1), Comment.class);
        Comment one = mongoTemplate.findOne(query, Comment.class);
        return one.getLikeCount();
    }

    /**
     * 取消点赞评论
     *
     * @param id
     * @return
     */
    @Override
    public Integer dislikeComment(ObjectId id) {
        Query query = new Query().addCriteria(Criteria.where("_id").is(id).and("commentType").is(CommentType.COMMENT.getType()));
        Update update = new Update();
        mongoTemplate.updateFirst(query, update.inc("likeCount",-1), Comment.class);
        Comment one = mongoTemplate.findOne(query, Comment.class);
        return one.getLikeCount();
    }

    /**
     *      获取评论
     * @param page
     * @param size
     * @param id
     * @return
     */
    @Override
    public PageResult<Comment> getCommentAll(Long page,Long size, Long id) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(id).and("commentType").is(CommentType.COMMENT.getType()));
        //判断数量是否超过10条
        long count = mongoTemplate.count(query, Comment.class);
        if (count > 10) {
            size = 10L;
            query.skip((page - 1) * size).limit(size.intValue());
        }
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return new PageResult<>(page, size, count, comments);
    }

    /**
     *      是否点赞
     * @param id
     * @param userId
     * @return
     */
    @Override
    public boolean isLike(ObjectId id, Long userId) {
        Query query = new Query().addCriteria(Criteria.where("publishId").is(id).and("commentType").is(CommentType.LIKE.getType()).and("userId").is(userId));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if (comment != null) {
            return true;
        }
        return false;
    }

    /**
     *      是否喜欢
     * @param id
     * @param userId
     * @return
     */
    @Override
    public boolean isLove(ObjectId id, Long userId) {
        Query query = new Query().addCriteria(Criteria.where("publishId").is(id).and("commentType").is(CommentType.LOVE.getType()).and("userId").is(userId));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if (comment != null) {
            return true;
        }
        return false;
    }

}
